package com.lw.vue_shop_server.service.impl;

import com.lw.vue_shop_server.mapper.ManagerMapper;
import com.lw.vue_shop_server.po.Manager;
import com.lw.vue_shop_server.service.ManagerService;
import com.lw.vue_shop_server.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ManagerServiceImpl implements ManagerService {

    @Autowired
    public ManagerMapper managerMapper;
    @Autowired
    public TokenUtil tokenUtil;

    @Override
    public Map findManagerByNameAndPwd(Map map) {
        Manager manager = managerMapper.findManagerByNameAndPwd((String)map.get("username"), (String)map.get("password"));
        // 业务处理
        Map<String,Object> result = new HashMap<>();
        Map<String,Object> meta = new HashMap<>();
        if(manager == null){
            meta.put("msg","参数错误");
            meta.put("status",400);
        }else {
            manager.setToken(tokenUtil.getToken(manager.getId().toString(),manager.getUsername()));
            result.put("data",manager);
            meta.put("msg","登录成功");
            meta.put("status",200);
        }
        result.put("meta",meta);
        return result;
    }

    @Override
    public Map getUserlist(Map map) {
        List<Manager> user = managerMapper.getUserlist((Integer) map.get("pagenum"),(Integer) map.get("pagesize"),map.get("query").toString());
        Map<String,Object> result = new HashMap<>();
        Map<String,Object> meta = new HashMap<>();
        Map<String,Object> data = new HashMap<>();
        if(user == null){
            meta.put("msg","查询失败");
            meta.put("status",400);
        }else {
            data.put("pagenum",map.get("pagenum"));
            Integer totalpage = managerMapper.getUserNum(map.get("query").toString());
            if(totalpage!=null){
                data.put("total",totalpage);
            }
            int pagenum = (Integer)map.get("pagenum");
            int pagesize =(Integer) map.get("pagesize");
            pagenum = (pagenum-1)*pagesize;
            data.put("users",user);
            meta.put("msg","获取成功");
            meta.put("status",200);
        }
        result.put("data",data);
        result.put("meta",meta);
        return result;
    }

    @Override
    public Integer getUserNum() {
        return managerMapper.getUserNum("");
    }

    @Override
    public Map addManager(Manager user) {
        Map<String,Object> result = new HashMap<>();
        Map<String,Object> meta = new HashMap<>();
        int row = managerMapper.addManager(user);
        if(row>0){
            meta.put("msg","用户创建成功");
            meta.put("status",201);
        }else {
            meta.put("msg","用户创建失败");
            meta.put("status",400);
        }
        result.put("meta",meta);
        return result;
    }

    @Override
    public Map upadteManager(Manager user) {
        Map<String,Object> result = new HashMap<>();
        Map<String,Object> meta = new HashMap<>();
        int row = managerMapper.upadteManager(user);
        if(row>0){
            Manager manager = managerMapper.findUserById(user.getId());
            if(manager!=null){
                result.put("data",manager);
                meta.put("msg","更新成功");
                meta.put("status",200);
            }else {
                meta.put("msg","更新成功，但查询失败");
                meta.put("status",404);
            }
        }else {
            meta.put("msg","更新失败");
            meta.put("status",500);
        }
        result.put("meta",meta);
        return result;
    }

    @Override
    public Map findUserById(Integer id) {
        Map<String,Object> result = new HashMap<>();
        Map<String,Object> meta = new HashMap<>();
        Manager manager = managerMapper.findUserById(id);
        if(manager != null){
            result.put("data",manager);
            meta.put("msg","查询成功");
            meta.put("status",200);
        }else {
            meta.put("msg","查询失败");
            meta.put("status",400);
        }
        result.put("meta",meta);
        return result;
    }

    @Override
    public Map delUserById(Integer id){
        Map<String,Object> result = new HashMap<>();
        Map<String,Object> meta = new HashMap<>();
        int row = managerMapper.delUserById(id);
        if(row>0){
            meta.put("msg","更新成功");
            meta.put("status",200);
        }else {
            meta.put("msg","更新失败");
            meta.put("status",501);
        }
        result.put("data",null);
        result.put("meta",meta);
        return result;
    }

    @Override
    public Map updateStateById(Manager manager){
        Map<String,Object> result = new HashMap<>();
        Map<String,Object> meta = new HashMap<>();
        int row = managerMapper.updateStateById(manager);
        if(row>0){
            Manager user = managerMapper.findUserById(manager.getId());
            result.put("data",user);
            meta.put("msg","更新状态成功");
            meta.put("status",200);
        }else {
            meta.put("msg","更新状态失败");
            meta.put("status",501);
        }
        result.put("meta",meta);
        return result;
    }

    @Override
    public Map updateRoleById(Manager manager){
        Map<String,Object> result = new HashMap<>();
        Map<String,Object> meta = new HashMap<>();
        int row = managerMapper.updateRoleById(manager);
        if(row>0){
            Manager user = managerMapper.findUserById(manager.getId());
            result.put("data",user);
            meta.put("msg","设置角色成功");
            meta.put("status",200);
        }else {
            meta.put("msg","设置角色失败");
            meta.put("status",501);
        }
        result.put("meta",meta);
        return result;
    }


}
